<?php
namespace app\api\controller;

use app\common\cache\agent\IAgentAdminCache;
use app\common\cache\user\IUserCache;
use app\common\controller\ApiBaseController;
use app\common\enum\IsPhone;
use app\common\enum\IsTrack;
use app\common\ext\IDb;
use app\common\ext\UserPoolBid;

class Bid extends ApiBaseController {

    /**
     * @功能：发布订单
     * @开发者：gys
     */
    public function add_to_pool(){
        //接收参数
        $province_id  = self::get_data('province_id',self::NOT_EMPTY,"省份编号不能为空！");
        $city_id  = self::get_data('city_id',self::NOT_EMPTY,"城市编号不能为空！");
        $money = self::get_data('money',self::NOT_EMPTY,"贷款金额不能为空！");
        $period = self::get_data('period',self::NOT_EMPTY,"贷款周期不能为空！");
        $purpose = self::get_data('purpose',self::NOT_EMPTY,"贷款目的不能为空！");
//        $bid_end = self::get_data('bid_end',self::NOT_EMPTY,"截止日期不能为空！");

        if(mb_strlen($purpose) > 4){
            self::set_code(self::WARNING_0001);
            self::set_msg('贷款目的不能大于4个字');
            self::send();
        }

        $user_pool_bid_id = \app\common\ext\User::addToBidPool(self::$uid,$province_id,$city_id,$money,$period,null,$purpose);
        if(!$user_pool_bid_id){
            self::set_code(self::WARNING_0002);
            self::set_msg("发布订单失败！");
            self::send();
        }

        //操作成功
        self::set_code(self::SUCCESS);
        self::set_msg("成功！");
        self::set_value("user_pool_bid_id",$user_pool_bid_id);
        self::send();
    }

    /**
     * @功能：订单列表
     * @开发者：gys
     */
    public function bid_list(){
        $pool_bid_status = self::get_data('pool_bid_status');
        $bottom_id = self::get_data('bottom_id');

        if(!empty($pool_bid_status)) $where['pool_bid_status'] = $pool_bid_status;
        if(!empty($bottom_id)) $where['concat(1,lpad(user_pool_bid_id,8,0))'] = array("lt",$bottom_id);

        $where['user_id'] = self::$uid;
        $list = IDb::getInstance('user_pool_bid')
            ->setDbFiled('user_pool_bid_id,bid_no,money,period,bid_num,pool_bid_status,create_time,bid_end')
            ->setDbWhere($where)
            ->setDbOrder("user_pool_bid_id desc")
            ->setDbLimit(10)
            ->sel();

        //整理数据
        if(!empty($list) && is_array($list) && count($list) > 0){
            //循环整理数据
            foreach($list as &$data){
                $data['create_time'] = substr($data['create_time'],0,10);
                $data['bid_end'] = substr($data['bid_end'],0,10);

                //设置排序编码
                $data['code'] = '1'.str_pad($data['user_pool_bid_id'],8,0,STR_PAD_LEFT);
            }
        }else{
            $list = [];
        }

        //操作成功
        self::set_code(self::SUCCESS);
        self::set_msg("成功！");
        self::set_value("bid_list",$list);
        self::send();
    }

    /**
     * @功能：订单详情
     * @开发者：gys
     */
    public function bid_info(){
        //接收参数
        $user_pool_bid_id  = self::get_data('user_pool_bid_id',self::NOT_EMPTY,"订单编号不能为空！");

        $where['user_pool_bid_id'] = $user_pool_bid_id;
        $where['user_id'] = self::$uid;
        $bid_info = IDb::getInstance('user_pool_bid')
            ->setDbWhere($where)
            ->row();
        if(empty($bid_info)){
            self::set_code(self::ERR_0001);
            self::set_msg("订单不存在！");
            self::send();
        }

        $user_info = IUserCache::getInfo($bid_info['user_id'],['real','sex']);
        $bid_info['real'] = get_real_cover($user_info['real'],$user_info['sex']);

        $agent_admin_info = IAgentAdminCache::getInfoForDisplay($bid_info['agent_admin_id']);

        //操作成功
        self::set_code(self::SUCCESS);
        self::set_msg("成功！");
        self::set_value("bid_info",$bid_info);
        self::set_value("agent_admin_info",$agent_admin_info);
        self::set_value("agent_admin_list",[$agent_admin_info]);
        self::send();
    }

    /**
     * @功能：订单跟踪
     * @开发者：gys
     */
    public function task_list(){
        //接收参数
        $user_pool_bid_id  = self::get_data('user_pool_bid_id',self::NOT_EMPTY,"订单编号不能为空！");
        $bottom_id = self::get_data('bottom_id');

        if(!empty($bottom_id)) $where['concat(1,lpad(user_pool_bid_task_id,8,0))'] = array("lt",$bottom_id);

        $where['user_pool_bid_id'] = $user_pool_bid_id;
        $where['task_type'] = IsTrack::Yes;
        $evaluate_list = IDb::getInstance('user_pool_bid_task up')
            ->setDbWhere($where)
            ->setDbOrder("user_pool_bid_task_id desc")
            ->setDbLimit(10)
            ->sel();

        foreach ($evaluate_list as &$val){
            //解析资源数据
            $val['video_list'] = empty($val['video_list']) ? [] : explode('{|}',$val['video_list']);
            $val['audio_list'] = empty($val['audio_list']) ? [] : explode('{|}',$val['audio_list']);
            $val['img_list'] = empty($val['img_list']) ? [] : explode('{|}',$val['img_list']);

            //设置排序编码
            $val['code'] = '1'.str_pad($val['user_pool_bid_task_id'],8,0,STR_PAD_LEFT);
        }

        //操作成功
        self::set_code(self::SUCCESS);
        self::set_msg("成功！");
        self::set_value("evaluate_list",$evaluate_list);
        self::send();
    }

    /**
     * @功能：获取投标经理
     * @开发者：gys
     */
    private function bid_admin_list(){
        //接收参数
        $user_pool_bid_id  = self::get_data('user_pool_bid_id',self::NOT_EMPTY,"订单编号不能为空！");

        $where['user_pool_bid_id'] = $user_pool_bid_id;
        $tender_list = IDb::getInstance('agent_admin_tender')
            ->setDbWhere($where)
            ->sel();
        if(is_array($tender_list)){
            foreach ($tender_list as &$val){
                $admin_info = IAgentAdminCache::getInfo($val['agent_admin_id'],['real','avatar','evaluate_rate','is_company','is_real','service_ote']);
                if(mb_strlen($val['service_ote']) > 25){
                    $admin_info['service_ote'] = mb_substr($val['service_ote'],0,25).'...';
                }
                $admin_info['is_phone'] = empty($admin_info['phone']) ? IsPhone::not : IsPhone::yes;

                $val = array_merge($admin_info,$val);
            }
        }else{
            $tender_list = [];
        }

        //操作成功
        self::set_code(self::SUCCESS);
        self::set_msg("成功！");
        self::set_value("tender_list",$tender_list);
        self::send();
    }

    /**
     * @功能：开标
     * @开发者：gys
     */
    private function bid_open(){
        //接收参数
        $user_pool_bid_id  = self::get_data('user_pool_bid_id',self::NOT_EMPTY,"订单编号不能为空！");
        $agent_admin_tender_id = self::get_data('agent_admin_tender_id',self::NOT_EMPTY,"订单编号不能为空！");

        $result = UserPoolBid::open(null,$user_pool_bid_id,$agent_admin_tender_id);
        if(!$result){
            self::set_code(self::ERR_0001);
            self::set_msg(UserPoolBid::getError());
            self::send();
        }

        //操作成功
        self::set_code(self::SUCCESS);
        self::set_msg("成功！");
        self::send();
    }

}
